package com.hwzn.module.business.service.impl;

import java.util.HashMap;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import com.hwzn.module.business.service.OcrService;
import com.hwzn.module.core.dos.business.ocr.Result;
import com.hwzn.module.core.enums.business.OcrEnum;

/**
 * ocr service implementation
 *
 * @author jiaobo
 * @date Created in 2024/5/21 16:25
 * @company hw
 **/
@Service
public class OcrServiceImpl implements OcrService, InitializingBean {

    @Resource
    private AipOcr client;

    private HashMap<String, String> options;

    @Override
    public Result ocrAnalysis(byte[] bytes) {
        JSONObject res = client.accurateGeneral(bytes, options);
        return JSON.parseObject(res.toString(2), Result.class);
    }

    @Override
    public void afterPropertiesSet() {
        HashMap<String, String> optionMap = new HashMap<>(4);
        optionMap.put(OcrEnum.RGO.getKey(), OcrEnum.RGO.getValue());
        optionMap.put(OcrEnum.DDO.getKey(), OcrEnum.DDO.getValue());
        optionMap.put(OcrEnum.VLO.getKey(), OcrEnum.VLO.getValue());
        optionMap.put(OcrEnum.PO.getKey(), OcrEnum.PO.getValue());
        this.options = optionMap;
    }

}
